<?php

namespace Worker\Controller;

use Common\Service\Validator;
use Think\Controller;
use Think\Log;
use Common\Service\Curl;
use Common\Service\Weixin;

class UserController extends BaseController
{
    /**
     * 师傅登录/绑定
     */
    public function login()
    {
        $mobile = I('mobile', '', 'trim');
        $password = I('password', '', 'trim');
        $openid = I('openid', '', 'trim');
        if (!Validator::checkPhone($mobile)) {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '手机号错误']);
        }

        if ($password == '') {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '请输入密码']);
        }

        if ($openid == '') {
            $this->ajaxReturn(['status' => false, 'code' => 1003, 'msg' => '非法登录']);
        }

        $worker = $this->chenkLogin('worker', $mobile, $password);
        if ($worker) {
            if ($worker['openid'] == '') {
                M('worker')->where(['mobile' => $mobile])->save(['openid' => $openid]);
            } else {
                if ($openid != $worker['openid']) {
                    $this->ajaxReturn(['status' => false, 'code' => 1005, 'msg' => '非法请求，请联系管理员']);
                }
            }

            //前面更新了数据需要重新获取一次
            $worker = M('worker')->where(['id' => $worker['id']])->find();
            $data = [
                'name' => $worker['name'],
                //'company_name' => $company_name,
            ];

            $file = M('files')->where(['file_id' => $worker['headimg']])->find();
            if ($file) {
                $file_path = C('DOMAIN') . $file['filepath'];
            } else {
                $file_path = $worker['headimg'];
            }

            $data['headimg'] = $file_path;
            $data['user_id'] = $worker['id'];
            $data['openid'] = $worker['openid'];
            $data['mobile'] = $worker['mobile'];
            $data['user_type'] = 3;
            $ret = ['status' => true, 'code' => 1000, 'msg' => '登陆成功', 'data' => $data];
            $this->ajaxReturn($ret);
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1004, 'msg' => '登录数据有误']);
        }
    }

    /**
     * 问题反馈,三个角色都可以使用
     */
    public function addFeedBack()
    {
        $arr = I('post.');
        $data['id'] = $arr['id'];
        $data['content'] = $arr['content'];
        $data['images'] = $arr['images'];
        $data['user_type'] = $this->getRoleTab()[$arr['user_type']];//audit,store,worker
        $data['add_time'] = time();

        $user = $this->checkUser($data['user_type'], $data['id']);
        if (!$user) {
            $this->ajaxReturn(['status' => false, 'code' => 1003, 'msg' => '用户信息有误', 'data' => []]);
        }

        if (!$data['content'] || !$data['user_type']) {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '请填写完整数据再添加', 'data' => []]);
        }
        $ret = M('feedback')->add($data);
        if ($ret) {
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '添加问题反馈成功', 'data' => []]);
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '服务器异常', 'data' => []]);
        }
    }
}